//
//  Checkout.m
//  Test
//
//  Created by admin on 10/31/12.
//  Copyright (c) 2012 Tonny Dam. All rights reserved.
//

#import "Checkout.h"
#import "SBJsonParser.h"
@implementation Checkout
#define SERVER_URL @"http://nghexaydung.com/whopaid/public/"
static Checkout *_instance = nil;  // <-- important
@synthesize _id, _user_id, _event_id, _enpenses_type_id, _amount, _description;
+(Checkout *)instance
{
	// skip everything
	if(_instance) return _instance;
    
	// Singleton
	@synchronized([Checkout class])
	{
		if(!_instance)
		{
			_instance = [[self alloc] init];
            
            //	NSLog(@"Creating global instance!"); <-- You should see this once only in your program
		}
        
		return _instance;
	}
    
	return nil;
}
-(NSMutableArray *)getCheckoutWS:(int)event_id
{
    NSMutableArray *listData = [[NSMutableArray alloc] init ];
    NSMutableArray *listUser = [[NSMutableArray alloc] init ];
    NSMutableArray *listAll = [[NSMutableArray alloc] init ];
    SBJsonParser *parser = [[SBJsonParser alloc] init];
    NSString *url = [NSString stringWithFormat:@"%@/checkout/getinfocheckout?event_id=%d",SERVER_URL, event_id];
    NSLog(@"get checkout: %@",url);
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
    NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
    NSDictionary *results = [parser objectWithString:json_string error:nil];
    NSDictionary *valueEvent = [results objectForKey:@"event"];
    EventClass *event = [[EventClass alloc] init];
    event.eventTitle = [valueEvent objectForKey:@"name"];
    event.totalAmount = [[valueEvent objectForKey:@"totalamount"] doubleValue];
    NSDictionary *checkout = [results objectForKey:@"checkout"];
    for (NSDictionary *obj in checkout)
    {
        NSString *objRow = [[NSString stringWithFormat:@"%d#%d#%@#%@#%@#%d#%d", [[obj objectForKey:@"id"] intValue], [[obj objectForKey:@"user_id"] intValue] , [obj objectForKey:@"user_name"], [obj objectForKey:@"member_name"],[obj objectForKey:@"amount"], [[obj objectForKey:@"quantity"] intValue], [[obj objectForKey:@"member_id"] intValue] ] retain];
        //[listData addObject:objRow];
        ParticipateClass *par = [[ParticipateClass alloc] init];
        par.userId = [[obj objectForKey:@"user_id"] intValue];
        par.name = [obj objectForKey:@"user_name"];
        par.image = [obj objectForKey:@"user_avatar"];
        par.idString = [obj objectForKey:@"user_facebookid"];
        par.firstMail = [obj objectForKey:@"user_email"];
        par.quantity = [[obj objectForKey:@"quantity"] intValue];
        par.price = [[obj objectForKey:@"amount"] doubleValue];
        par.memberId = [[obj objectForKey:@"member_id"] intValue];
        par.memberName = [obj objectForKey:@"member_name"];
        par.memberAvatar = [obj objectForKey:@"member_avatar"];
        par.memberEmail = [obj objectForKey:@"member_email"];
        par.memberIdString = [obj objectForKey:@"member_facebookid"];
        [listData addObject:objRow];
        [listUser addObject:par];
    }
    [listAll addObject:event];
    [listAll addObject:listData];
    [listAll addObject:listUser];
    NSLog(@"number row in checkout %d", [listData count]);
    return listAll;
}

-(NSMutableArray *)getDatasWS:(int)event_id
{
    NSMutableArray *listData = [[NSMutableArray alloc] init ];
    SBJsonParser *parser = [[SBJsonParser alloc] init];
    NSString *url = [NSString stringWithFormat:@"%@/checkout/getlistchekout?event_id=%d",SERVER_URL, event_id];
    NSLog(@"get checkout list: %@",url);
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
    NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
    NSArray *results = [parser objectWithString:json_string error:nil];
    for (NSDictionary *obj in results)
    {
        NSString *ch = [NSString stringWithFormat:@"%d#%@#%@",[[obj objectForKey:@"id"] intValue], [obj objectForKey:@"date"], [obj objectForKey:@"username"]];
        NSLog(@"link %@",ch);
        [listData addObject: ch];
    }
    return listData;
}

-(NSMutableArray *) getDetailWS:(int)checkout_id
{
    Helper *helper = [Helper instance];
    NSMutableArray *listData = [[NSMutableArray alloc] init ];
    SBJsonParser *parser = [[SBJsonParser alloc] init];
    NSString *url = [NSString stringWithFormat:@"%@checkout/getchekoutinfo?checkout_id=%d",SERVER_URL,checkout_id];
    NSLog(@"get checkout detail %@",url);
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
    NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
    NSArray *results = [parser objectWithString:json_string error:nil];
    NSString *amount = @"";
    NSString *member_amount = @"";
    for (NSDictionary *obj in results)
    {   
        amount = [helper stringToMoney:[[obj objectForKey:@"amount"] doubleValue]];
        member_amount = [helper stringToMoney:[[obj objectForKey:@"member_amount"] doubleValue]];
        NSString *objRow = [NSString stringWithFormat:@"%d#%@#%@#%d#%@#%d#%@#%d", [[obj objectForKey:@"user_id"] intValue], [obj objectForKey:@"user_name"], amount , 1, member_amount, [[obj objectForKey:@"status"] intValue],[obj objectForKey:@"member_name"], [[obj objectForKey:@"member_id"] intValue] ];
        [listData addObject:objRow];
        
    }
    return listData;
}    

-(int) addCheckoutWS:(int)user_id EventId:(int)event_id ArrayExpenses:(NSMutableArray *)listExpenses ArrayParticipate:(NSMutableArray *)listParticipate
{
    NSString *url = [NSString stringWithFormat:@"%@checkout/addinfocheckout?event_id=%d&user_id=%d",SERVER_URL,event_id, user_id];
    //NSString *contents = [NSString stringWithContentsOfURL:[NSURL URLWithString:url]];
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
    [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    return 0;
}
@end
